﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using SAM.Framework.Plugins;
using SAM.Framework.Javascript;

namespace SAM.JavaScript.V8
{
    public class V8ExecuteForThirdPart : V8Execute, IPlugin
    {
        private IList<string> thirdFilePaths;

        /// <summary>
        /// 构造
        /// </summary>
        /// <param name="thridPart"></param>
        public V8ExecuteForThirdPart(string[] thirdPart)
            :this(AppDomain.CurrentDomain.SetupInformation.ApplicationBase, thirdPart)
        {
            
        }

        public V8ExecuteForThirdPart(string prefix, string[] thirdPart)
        {
            this.thirdFilePaths = new List<string>();
            foreach(var part in thirdPart)
            {
                this.thirdFilePaths.Add(System.IO.Path.Combine(prefix, part));
            }
        }

        public bool Uninstall()
        {
            this.Dispose();
            return true;
        }


        public bool Install()
        {
            foreach(var file in this.thirdFilePaths)
            {
                string code = this.ReadFile(file);
                if (string.IsNullOrWhiteSpace(code)) continue;
                this.ExecuteNonResult(code);
            }
            return true;
        }

        private string ReadFile(string fileFullPath)
        {
            string txt = System.IO.File.ReadAllText(fileFullPath, Encoding.UTF8);
            return txt;
        }
    }
}
